Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
requestretry
Advanced tools
request-retry wrap nodejs request to retry http(s) requests in case of error
The requestretry npm package is a wrapper around the popular request library that adds automatic retry functionality for failed HTTP requests. It allows you to specify retry strategies, including the number of retries, delay between retries, and conditions under which to retry.
Basic HTTP Request with Retry
This feature allows you to make an HTTP GET request with automatic retries. You can specify the maximum number of attempts, the delay between retries, and the retry strategy.
const request = require('requestretry');
request({
url: 'https://api.example.com/data',
method: 'GET',
maxAttempts: 5, // (default) try 5 times
retryDelay: 5000, // (default) wait for 5s before trying again
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}, function (err, response, body) {
if (err) {
console.error('Failed:', err);
} else {
console.log('Success:', body);
}
});
Custom Retry Strategy
This feature allows you to define a custom retry strategy. In this example, the request will be retried if there is an error or if the response status code is 502 (Bad Gateway).
const request = require('requestretry');
function myRetryStrategy(err, response, body) {
// retry the request if we had an error or if the response was a 'Bad Gateway'
return err || response.statusCode === 502;
}
request({
url: 'https://api.example.com/data',
method: 'GET',
maxAttempts: 3,
retryDelay: 3000,
retryStrategy: myRetryStrategy
}, function (err, response, body) {
if (err) {
console.error('Failed:', err);
} else {
console.log('Success:', body);
}
});
POST Request with Retry
This feature demonstrates how to make an HTTP POST request with automatic retries. The request includes a JSON body and will retry up to 4 times with a 2-second delay between attempts.
const request = require('requestretry');
request({
url: 'https://api.example.com/data',
method: 'POST',
json: true,
body: { key: 'value' },
maxAttempts: 4,
retryDelay: 2000,
retryStrategy: request.RetryStrategies.HTTPOrNetworkError
}, function (err, response, body) {
if (err) {
console.error('Failed:', err);
} else {
console.log('Success:', body);
}
});
axios-retry is a plugin for the axios HTTP client that adds retry functionality. It allows you to specify the number of retries, delay between retries, and conditions for retrying. Compared to requestretry, axios-retry is built on top of axios, which is a more modern and widely-used HTTP client than request.
got-retry is a plugin for the got HTTP client that adds retry functionality. It provides a flexible retry mechanism with customizable options for retry count, delay, and conditions. got-retry is built on top of got, which is known for its simplicity and performance, making it a good alternative to requestretry.
superagent-retry is a plugin for the superagent HTTP client that adds retry functionality. It allows you to specify the number of retries and conditions for retrying requests. superagent-retry is useful if you are already using superagent and need retry capabilities similar to those provided by requestretry.
request-retry - HTTP(s) request retry on recoverable errors.
When the connection fails with one of ECONNRESET
, ENOTFOUND
, ESOCKETTIMEDOUT
, ETIMEDOUT
, ECONNREFUSED
, EHOSTUNREACH
, EPIPE
, EAI_AGAIN
or when an HTTP 5xx or 429 error occurrs, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry.
❤️ Shameless plug
- Charts, simple as a URL. No more server-side rendering pain, 1 url = 1 chart
- Managed Keycloak IAM ? Try Cloud-IAM
- Mailpopin - Stripe payment emails you can actually use
Install with npm.
npm install --save requestretry
Request-retry is a drop-in replacement for request but adds two new options maxAttempts
and retryDelay
. It also adds one property to the response (or the error object, upon a network error), attempts
. It supports callbacks or promises.
var request = require('requestretry');
request({
url: 'https://api.domain.com/v1/a/b',
json: true,
// The below parameters are specific to request-retry
maxAttempts: 5, // (default) try 5 times
retryDelay: 5000, // (default) wait for 5s before trying again
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}, function(err, response, body){
// this callback will only be called when the request succeeded or after maxAttempts or on error
if (response) {
console.log('The number of request attempts: ' + response.attempts);
}
});
When you're using promises, you can pass the two following options:
fullResponse
(default true) - To resolve the promise with the full response or just the bodypromiseFactory
(default whenjs) - A function to allow the usage of a different promise implementation libraryrequest({
url: 'https://api.domain.com/v1/a/b',
json: true,
fullResponse: true // (default) To resolve the promise with the full response or just the body
})
.then(function (response) {
// response = The full response object or just the body
})
.catch(function(error) {
// error = Any occurred error
})
Using promiseFactory
option to use a different promise implementation library
// See the tests for different libraries usage examples
/**
* @param {Function} resolver The promise resolver function
* @return {Object} The promise instance
*/
function customPromiseFactory(resolver) {
// With when.js
return require('when').promise(resolver);
// With RSVP.js
var Promise = require('rsvp').Promise;
return new Promise(resolver);
}
request({
url: 'https://api.domain.com/v1/a/b',
json: true,
// Custom promise factory function
promiseFactory: customPromiseFactory
})
.then(function (response) {
// response = The full response object or just the body
})
.catch(function(error) {
// error = Any occurred error
})
A retry strategy let you specify when request-retry should retry a request
/**
* @param {Null | Object} err
* @param {Object} response
* @param {Object} body
* @param {Object} options copy
* @return {Boolean} true if the request should be retried
*/
function myRetryStrategy(err, response, body, options){
// retry the request if we had an error or if the response was a 'Bad Gateway'
return !!err || response.statusCode === 502;
}
/**
* @param {Null | Object} err
* @param {Object} response
* @param {Object} body
* @param {Object} options copy
* @return {Object} mustRetry: {Boolean} true if the request should be retried
* options: {Object} new options for request
*/
function myRetryStrategy(err, response, body, options){
options.url = 'new url'; //you can overwrite some attributes or create new object
return {
mustRetry: !!err || response.statusCode === 502,
options: options, //then it should be passed back, it will be used for new requests
}
}
/**
* With an asynchronous retry strategy
* @param {Null | Object} err
* @param {Object} response
* @param {Object} body
* @param {Object} options copy
* @return {Object} mustRetry: {Boolean} true if the request should be retried
* options: {Object} new options for request
*/
async function myRetryStrategy(err, response, body, options){
let token = await getNewApiAuthToken();
options.headers = {'Authorization': `Bearer ${token}`}
return {
mustRetry: true,
options: options, // retry with new auth token
}
}
request({
url: 'https://api.domain.com/v1/a/b'
json:true,
retryStrategy: myRetryStrategy
}, function(err, response, body){
// this callback will only be called when the request succeeded or after maxAttempts or on error
});
A delay strategy let you specify how long request-retry should wait before trying again the request
/**
* @param {Null | Object} err
* @param {Object} response
* @param {Object} body
* @return {Number} number of milliseconds to wait before trying again the request
*/
function myDelayStrategy(err, response, body){
// set delay of retry to a random number between 500 and 3500 ms
return Math.floor(Math.random() * (3500 - 500 + 1) + 500);
}
request({
url: 'https://api.domain.com/v1/a/b'
json:true,
delayStrategy: myDelayStrategy // delayStrategy is called 1 less times than the maxAttempts set
}, function(err, response, body){
// this callback will only be called when the request succeeded or after maxAttempts or on error
});
Here is how to implement an exponential backoff strategy:
/**
* @param {Number} attempts The number of times that the request has been attempted.
* @return {Number} number of milliseconds to wait before retrying again the request.
*/
function getExponentialBackoff(attempts) {
return (Math.pow(2, attempts) * 100) + Math.floor(Math.random() * 50);
}
function constructExponentialBackoffStrategy() {
let attempts = 0;
return () => {
attempts += 1;
return getExponentialBackoff(attempts);
};
}
request({
url: 'https://api.domain.com/v1/a/b'
json:true,
delayStrategy: constructExponentialBackoffStrategy() // need to invoke the function to return the closure.
}, function(err, response, body){
// this callback will only be called when the request succeeded or after maxAttempts or on error
});
You can access to the underlying request
library thanks to request.Request
:
const request = require('requestretry');
console.log(request.Request); // original request library
Thus, if needed, it's possible to monkey-patch or extend the underlying Request library:
request.Request = class extends request.Request {
constructor(url, options, f, retryConfig) {
super(url, options, f, retryConfig);
// this constructor will be called for every requestretry call,
// and give you global logging
console.log('Request', url, options, f, retryConfig);
}
}
request
optionsYou can use the defaults
method to provide default options like so:
var request = require('requestretry').defaults({ json: true, retryStrategy: myRetryStrategy });
As with request
, several helpers are provided for various HTTP methods and usage:
request(options [, callback])
.request(url [, callback])
- same as request(options [, callback])
.request(url, options [, callback])
- same as request(options [, callback])
.request.get(url [, callback])
- same as request(options [, callback])
, defaults options.method
to GET
.request.get(url, options [, callback])
- same as request(options [, callback])
, defaults options.method
to GET
.request.head(url)
- same as request(options [, callback])
, defaults options.method
to HEAD
.request.post(url)
- same as request(options [, callback])
, defaults options.method
to POST
.request.put(url)
- same as request(options [, callback])
, defaults options.method
to PUT
.request.patch(url)
- same as request(options [, callback])
, defaults options.method
to PATCH
.request.del(url)
- same as request(options [, callback])
, defaults options.method
to DELETE
.request.delete(url)
- same as request(options [, callback])
, defaults options.method
to DELETE
.I maintain this project in my free time, if it helped you, well, I would be grateful to buy a beer thanks to your paypal or Bitcoins, donation!
FAQs
request-retry wrap nodejs request to retry http(s) requests in case of error
The npm package requestretry receives a total of 243,309 weekly downloads. As such, requestretry popularity was classified as popular.
We found that requestretry demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.